package com.ssm.car.server.controller.parkinglot;

import com.github.pagehelper.PageInfo;
import com.jee.ssm.common.support.Const;
import com.jee.ssm.common.utils.UUIDFactory;
import com.jee.ssm.common.web.json.Tip;
import com.jee.ssm.common.web.param.ParamMap;
import com.ssm.car.dao.model.Lock;
import com.ssm.car.dao.model.Parking;
import com.ssm.car.dao.model.Parkinglot;
import com.ssm.car.dao.model.Parkingzone;
import com.ssm.car.rpc.api.lock.LockService;
import com.ssm.car.rpc.api.parking.ParkingService;
import com.ssm.car.rpc.api.parkinglot.ParkinglotService;
import com.ssm.car.rpc.api.parkingzone.ParkingzoneService;
import com.ssm.upms.dao.model.Account;
import com.ssm.upms.log.AdminControllerLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 2017年5月23日  闫增宝
* 停车位管理 Controller
* @author GaoXiang
* @version 1.0
*/
@Controller
@RequestMapping("/parkinglot")
public class ParkinglotController  {


    /**
     * 进入停车位添加页面 携带一个生成的id --> longId
     * @param model 返回的实体容器
     * @return 添加页面
     */
    @RequestMapping(value="/add")
    @RequiresPermissions("parkinglot:add")
    public String add(ModelMap model, HttpServletRequest request){
/*
        HttpSession session = request.getSession();
        Account account = (Account)session.getAttribute(Const.ACCOUNT);
        if (account.getType() == 6){
            Parking parking = parkingService.selectById(account.getInfoId());
            model.put("parking",parking);
            Parkingzone pz = new Parkingzone();
            pz.setParkingId(account.getInfoId());
            model.put("zone",  parkingzoneService.zone(pz));
              model.put("type",account.getType());
        }*/



        model.put("longId", UUIDFactory.getStringId());
        return "manager/parkinglot/add";
    }


    /**
     * 进入停车位编辑页面
     * @param model 返回停车位的容器
     * @param id 停车位id
     * @return 编辑页面
     */
    @RequestMapping(value="/edit")
    @RequiresPermissions("parkinglot:edit")
    public String edit(HttpServletRequest request, ModelMap model, @RequestParam String id) {
      /*  Parkinglot parkinglot = parkinglotService.selectById(id);
        Parkingzone pz = new Parkingzone();
        pz.setParkingId(parkinglot.getParkingId());
        model.put("zone",  parkingzoneService.zone(pz));

        if(parkinglot.getLockId() != null && !"".equals(parkinglot.getLockId())){
            parkinglot.setLock(lockService.selectById(parkinglot.getLockId()));
        }
        HttpSession session= request.getSession();
        Account account= (Account) session.getAttribute(Const.ACCOUNT);
        parkinglot.setParking(parkingService.selectById(parkinglot.getParkingId()));

        Image img = new Image();
        img.setInfoId(parkinglot.getZone());
        img.setType("5");
        Image image = imageService.selectByInfoId5(img);
        parkinglot.setUrl(image.getUrl());
        model.put("type",account.getType());
        model.put("data",parkinglot);*/
        return "manager/parkinglot/edit";
    }


    /**
     * 停车位添加
     * 2017-06-02 10:07:54 韩榕榕
     * @param parkinglot 带id的停车位对象
     * @return 成功状态
     */
    @RequestMapping(value="/save")
    @ResponseBody
    @RequiresPermissions("parkinglot:add")
    @AdminControllerLog(description = "添加停车位" )
    public Tip save(Parkinglot parkinglot)  {

        try {
            if(parkinglotService.countParkinglot(parkinglot)>0){
                return  new Tip(1,"停车车位序号已存在");
            }
            parkinglot.setSetTime(new Date());
            parkinglotService.insert(parkinglot);
            return new Tip();
        } catch (Exception e) {
            e.printStackTrace();
            return new Tip(1,"添加失败！");
        }

    }


    /**
     * 根据 id 修改停车位
     * 2017-6-2 10:23:27 韩榕榕
     * @param parkinglot 带id的停车位对象
     * @return 成功状态
     */
    @RequestMapping(value="/update")
    @ResponseBody
    @RequiresPermissions("parkinglot:edit")
    @AdminControllerLog(description = "修改停车位" )
    public Tip update(Parkinglot parkinglot,@RequestParam String id) {
        try {
            Parkinglot p=parkinglotService.selectById(id);
            if(p.getNumber()!=parkinglot.getNumber()){
                if(parkinglotService.countParkinglot(parkinglot)>0){
                    return  new Tip(1,"车位序号已存在");
                }
            }
            parkinglot.setSetTime(new Date());
            parkinglotService.updateById(parkinglot);
            return new Tip();
        } catch (Exception e) {
            e.printStackTrace();
            return new Tip(1,"修改失败！");
        }

    }


    /**
     * 根据 id 删除停车位
     * @param id 停车位id
     * @return 成功状态
     */
    @RequestMapping(value="/delete")
    @ResponseBody
    @RequiresPermissions("parkinglot:delete")
    @AdminControllerLog(description = "删除停车位" )
    public Tip delete(@RequestParam String id) {
        try {
            parkinglotService.deleteById(id);
            return new Tip();
        } catch (Exception e) {
            e.printStackTrace();
            return new Tip(1,"删除失败！");
        }

    }


    /**
     * 根据 id 列表批量删除
     * @param ids 停车位id List
     * @return 成功状态
     */
    @RequestMapping(value="/deleteByIds")
    @ResponseBody
    @RequiresPermissions("parkinglot:delete")
    @AdminControllerLog(description = "批量删除停车位" )
    public Tip deleteByIds(@RequestParam("ids") List<String> ids) {
        try {
            List<Parkinglot> parkinglots=parkinglotService.selectByIds(ids);
            List<String> lockIds=new ArrayList<String>();
            for(Parkinglot p:parkinglots){
               if(p.getLockId() !=null || p.getLockId() !=""){
                   lockIds.add(p.getLockId());
               }
            }
            if(lockIds.size()>0){
                lockService.deleteByIds(lockIds);
            }
            parkinglotService.deleteByIds(ids);
            return new Tip();
        } catch (Exception e) {
            //e.printStackTrace();
            return new Tip(1,"批量删除失败！");
        }

    }


    /**
     * 根据 id 查找停车位
     * @param id 停车位id
     * @return 停车位对象 json
     */
    @RequestMapping(value="/findJson")
    @ResponseBody
    @RequiresPermissions("parkinglot:list")
    public Parkinglot find(@RequestParam String id) {

        return parkinglotService.selectById(id);
    }


    /**
     * 获取停车位列表 获取全部 不分页
     * @param request 请求参数
     * @return 停车位列表页面
     */
    @RequestMapping(value="/all")
    @RequiresPermissions("parkinglot:list")
    public String all(HttpServletRequest request, ModelMap modelMap) {

        return list(request,modelMap,1,0);
    }



    /**
     * 获取停车位列表 分页
     * 2017-5-25 11:44:21 韩榕榕
     * @param request 请求参数
     * @param page 第几页
     * @param size 每页大小
     * @return 停车位列表页面
     */
    @RequestMapping(value="/list")
    @RequiresPermissions("parkinglot:list")
    public String list(HttpServletRequest request, ModelMap modelMap, Integer page, Integer size) {
       HttpSession session= request.getSession();
       Account account= (Account) session.getAttribute(Const.ACCOUNT);
        ParamMap paramMap = new ParamMap(request);
        if(account.getType() == 6) {
            paramMap.put("parkingId", account.getInfoId());
        }else if(account.getType() == 8) {
            paramMap.put("officeId", account.getInfoId());
        }
        PageInfo<Parkinglot> pageInfo = parkinglotService.list(paramMap,page,size);
    /*    for(Parkinglot parkinglot:pageInfo.getList()){
            if(parkinglot.getLockId() != null && !"".equals(parkinglot.getLockId())){
                parkinglot.setLock(lockService.selectById(parkinglot.getLockId()));
            }
            parkinglot.setParking(parkingService.selectById(parkinglot.getParkingId()));
        }*/
        modelMap.put("parking",parkingService.listAll(null));
        modelMap.put("type",account.getInfoId());
        modelMap.put("pageInfo",pageInfo);
        modelMap.putAll(paramMap);
        return "manager/parkinglot/list";
    }


    /**
     * 2017年5月23日 闫增宝  选择地锁
     * @param request
     * @param modelMap
     * @return
     */
/*
    @RequestMapping(value="/chooselock")
    public String chooseLock(HttpServletRequest request, ModelMap modelMap) {

        ParamMap paramMap = new ParamMap(request);
        List<Lock> lockList = new ArrayList<>();
        paramMap.put("status",0);
        PageInfo<Lock> pageInfo = lockService.list(paramMap);//所有地锁
        for (Lock lock:pageInfo.getList()) {
            Parkinglot p = parkinglotService.selectByLock(lock);
            if(p == null){
                lockList.add(lock);
            }
        }
        modelMap.put("pageInfo",lockList);
        modelMap.putAll(paramMap);
        return "manager/parkinglot/chooseLock";
    }
*/


    /**
     * 查询这个停车场所有分区
     * @param id 停车位id
     * @return 分区
     */
    @RequestMapping(value="/zone")
    @ResponseBody
    public List<Parkingzone> zone(String id) {
        Parkingzone pz = new Parkingzone();
        pz.setParkingId(id);
        List<Parkingzone> zone =parkingzoneService.zone(pz);
        return zone;
    }

    /**
     * 查询这个分区的图片
     * @param id 停车位id
     * @return 分区
     */
    @RequestMapping(value="/findZoneImg")
    @ResponseBody
    public Parkinglot findZoneImg(String id) {
        Parkinglot lot =parkinglotService.findZoneImg(id);
        return lot;
    }

    /**
     * 2017年6月12日 姜延鹏 选择车位位置
     * @param src
     * @return
     */
    @RequestMapping(value = "selectParklot")
    public String selectParklot(String src,ModelMap modelMap){
        modelMap.put("src",src);
        return "manager/parkinglot/selectParklot";
    }

    //---------------------------- property -------------------------------

    @Resource
    private ParkinglotService parkinglotService;

    @Resource
    private LockService lockService;

    @Resource
    private ParkingService parkingService;


    @Resource
    private ParkingzoneService parkingzoneService;

 /*   @Resource
    private ImageService imageService;*/

}
